﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Editors.Controls
{
    public partial class Inspection : DialogEditBase
    {
        public Inspection()
        {
            InitializeComponent();
            
            BindCombos();
        }

        private ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorInspVisitXmissionStructuresResultsXNoteEntity> m_XmissionNotes;
        private ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorDistributionResultEntity> m_DistributionNotes;

        public Common.Enumerations.DataTypes DataType { get; private set; }

        private void BindCombos()
        {
            if (Program.LookupTableData != null)
            {
                ComboOptions.InspectionResults(comboBoxResult);
                ComboOptions.InspectionTypes(comboBoxInspectionType);

                listSelectorNotes.DisplayMember = EntityLibrary.VendorInspVisitXmissionStructuresNoteEnumerations.Columns.Name.ToString();
                listSelectorNotes.ValueMember = EntityLibrary.VendorInspVisitXmissionStructuresNoteEnumerations.Columns.Vendor_InspVisit_XmissionStructures_NoteId.ToString();
                if (EditAllowed)
                    listSelectorNotes.DataSource = Program.LookupTableData.VendorInspVisitXmissionStructuresNotes;
                else
                    listSelectorNotes.Clear();


                //TODO: need to fill dropdown for inspector and company


                //comboBoxExNoteType.DisplayMember = EntityLibrary.VendorInspVisitXmissionStructuresNoteEnumerations.Columns.Name.ToString();
                //comboBoxExNoteType.ValueMember = EntityLibrary.VendorInspVisitXmissionStructuresNoteEnumerations.Columns.Vendor_InspVisit_XmissionStructures_NoteId.ToString();
                //comboBoxExNoteType.DataSource = Program.LookupTableData.VendorInspVisitXmissionStructuresNotes;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Data
        {
            get
            {
                return bindingSourcePole.DataSource;
            }
            set
            {
                bindingSourcePole.SuspendBinding();
                bindingSourceInspection.SuspendBinding();
                SetupDatabindings(value);
                bindingSourcePole.DataSource = value;
                bindingSourceInspection.ResumeBinding(); 
                bindingSourcePole.ResumeBinding();
            }
        }

        [DefaultValue(false)]
        public bool EditAllowed { get; set; }

        private void SetupDatabindings(object data)
        {
            if (data.GetType() == typeof(PolePosition.v3.EntityLibrary.VendorInspVisitXmissionStructuresPoleEntity))
            {
                DataType = Common.Enumerations.DataTypes.ProjectsXmissionInspection;

                this.bindingSourcePole.DataSource = typeof(PolePosition.v3.EntityLibrary.VendorInspVisitXmissionStructuresPoleEntity);
                
                this.textBoxInspectionDate.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "AcquisitionDate", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxCompany.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "CompanyFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxInspector.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "InspectorFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxCompany.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "VendorName", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxInspector.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "VendorContactName", true, DataSourceUpdateMode.OnPropertyChanged));

                EntityLibrary.VendorInspVisitXmissionStructuresPoleEntity _VendorInspVisitXmission;
                _VendorInspVisitXmission = data as EntityLibrary.VendorInspVisitXmissionStructuresPoleEntity;

                if (_VendorInspVisitXmission.VendorInspVisitXmissionStructuresResultCollection != null &&
                    _VendorInspVisitXmission.VendorInspVisitXmissionStructuresResultCollection.Count() > 0)
                {
                    this.bindingSourceInspection.DataSource = typeof(PolePosition.v3.EntityLibrary.VendorInspVisitXmissionStructuresResultEntity);

                    this.comboBoxInspectionType.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourceInspection, "Vendor_InspVisit_XmissionStructures_Inspection_TypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                    this.comboBoxResult.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourceInspection, "Vendor_InspVisit_XmissionStructures_ResultTypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                    this.textBoxComments.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourceInspection, "Comments", true, DataSourceUpdateMode.OnPropertyChanged));
                    this.checkBoxElectricalProblem.DataBindings.Add(new System.Windows.Forms.Binding("Checked", this.bindingSourceInspection, "bElectrical", true, DataSourceUpdateMode.OnPropertyChanged));
                    

                    this.bindingSourceInspection.DataSource = _VendorInspVisitXmission.VendorInspVisitXmissionStructuresResultCollection[0];

                    LoadNotesAsynch(_VendorInspVisitXmission.VendorInspVisitXmissionStructuresResultCollection[0].Vendor_InspVisit_XmissionStructures_ResultId.Value);
                }
            }
            else if (data.GetType() == typeof(PolePosition.v3.EntityLibrary.VendorDistributionPoleEntity))
            {
                DataType = Common.Enumerations.DataTypes.ProjectsDistributionInspection;

                this.bindingSourcePole.DataSource = typeof(EntityLibrary.VendorDistributionPoleEntity);

                this.textBoxInspectionDate.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "AcquisitionDate", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxCompany.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "VendorFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxInspector.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "InspectorFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxCompany.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "VendorName", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxInspector.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "VendorContactName", true, DataSourceUpdateMode.OnPropertyChanged));

                //NOTES - tb_Vendor_Distribution_Results

                EntityLibrary.VendorDistributionPoleEntity _VendorDistributionPole;
                _VendorDistributionPole = data as EntityLibrary.VendorDistributionPoleEntity;

                if (_VendorDistributionPole.VendorDistributionInspectionResultCollection != null &&
                    _VendorDistributionPole.VendorDistributionInspectionResultCollection.Count() > 0)
                {
                    this.bindingSourceInspection.DataSource = typeof(PolePosition.v3.EntityLibrary.VendorDistributionInspectionResultEntity);

                    this.comboBoxInspectionType.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourceInspection, "Vendor_Distribution_TypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                    this.comboBoxResult.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourceInspection, "Vendor_Distribution_ResultTypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                    this.textBoxComments.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourceInspection, "Comments", true, DataSourceUpdateMode.OnPropertyChanged));
                    this.checkBoxElectricalProblem.DataBindings.Add(new System.Windows.Forms.Binding("Checked", this.bindingSourceInspection, "BElectrical", true, DataSourceUpdateMode.OnPropertyChanged));

                    this.bindingSourceInspection.DataSource = _VendorDistributionPole.VendorDistributionInspectionResultCollection[0];

                    LoadNotesAsynch(_VendorDistributionPole.Vendor_Distribution_Poles_PoleId.Value);
                }
            }
        }

        private void LoadNotesAsynch(int resultId)
        {
            listSelectorNotes.LoadingVisible = true;
            backgroundWorkerNotes.RunWorkerAsync(resultId);
        }

        private void backgroundWorkerNotes_DoWork(object sender, DoWorkEventArgs e)
        {
            //System.Threading.Thread.Sleep(1000);

            if (DataType == Common.Enumerations.DataTypes.ProjectsXmissionInspection)
                m_XmissionNotes = ServiceProxy.UtilityService.GetXmissionInspectionProjectNotesForResult((int)e.Argument);
            else if (DataType == Common.Enumerations.DataTypes.ProjectsDistributionInspection)
                m_DistributionNotes = ServiceProxy.UtilityService.GetDistributionInspectionProjectNotesForResult((int)e.Argument);

            if (backgroundWorkerNotes.CancellationPending)
                e.Cancel = true;
        }

        private void backgroundWorkerNotes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            listSelectorNotes.LoadingVisible = false;

            if (e.Cancelled)
                return;

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("A Data Retrieval Error has occurred attempting to load the Notes. Please close editor and try again.", "Data Retrieval Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                listSelectorNotes.Enabled = false;
                return;
            }

            if (DataType == Common.Enumerations.DataTypes.ProjectsXmissionInspection)
            {
                if (m_XmissionNotes != null)
                {
                    if (EditAllowed)
                    {
                        var _NoteValues = from notes in m_XmissionNotes
                                          select notes.Vendor_InspVisit_XmissionStructures_NoteFk.Value;
                        listSelectorNotes.SetSelectedValues<int>(_NoteValues.ToList<int>());
                    }
                    else
                    {
                        if (m_XmissionNotes.Count() > 0)
                        {

                            //this will only show selected items
                            var _NoteValues = from notes in m_XmissionNotes
                                              join lookupdata in Program.LookupTableData.VendorInspVisitXmissionStructuresNotes
                                              on notes.Vendor_InspVisit_XmissionStructures_NoteFk equals lookupdata.Vendor_InspVisit_XmissionStructures_NoteId
                                              select lookupdata;

                            listSelectorNotes.ReadOnlyNoneSelectedVisible = false;
                            listSelectorNotes.DataSource = _NoteValues.ToList();
                        }
                        else
                        {
                            listSelectorNotes.ReadOnlyNoneSelectedVisible = true;
                        }
                    }

                
                }
            }
            if (DataType == Common.Enumerations.DataTypes.ProjectsDistributionInspection)
            {
                if (m_DistributionNotes != null)
                {
                    if (EditAllowed)
                    {
                        var _NoteValues = from notes in m_DistributionNotes
                                          select notes.Vendor_Distribution_ResultTypeFk.Value;
                        listSelectorNotes.SetSelectedValues<int>(_NoteValues.ToList<int>());
                    }
                    else
                    {
                        if (m_DistributionNotes.Count() > 0)
                        {

                            //this will only show selected items
                            var _NoteValues = from notes in m_DistributionNotes
                                              join lookupdata in Program.LookupTableData.VendorInspVisitXmissionStructuresNotes
                                              on notes.Vendor_Distribution_ResultTypeFk equals lookupdata.Vendor_InspVisit_XmissionStructures_NoteId
                                              select lookupdata;

                            listSelectorNotes.ReadOnlyNoneSelectedVisible = false;
                            listSelectorNotes.DataSource = _NoteValues.ToList();
                        }
                        else
                        {
                            listSelectorNotes.ReadOnlyNoneSelectedVisible = true;
                        }
                    }


                }
            }


            listSelectorNotes.Enabled = true;
        }

    }
}
